﻿#include "SetTurnTableAndThermostatDialog.h"

#include <qregexp.h>

#include "TurnTableRunningStateManager.h"
#include "SerialPortManager.h"
#include "GroupBox_SetSerialPort.h"
#include "LineEdit_TurnTableRate.h"

SetTurnTableAndThermostatDialog::SetTurnTableAndThermostatDialog(QWidget* parent) :QDialog(parent)
{
  ui.setupUi(this);
  setFixedSize(this->size());
  connect(ui.pushButton_confirm, &QPushButton::clicked, this, &SetTurnTableAndThermostatDialog::accept);
  connect(ui.pushButton_cancel, &QPushButton::clicked, this, &SetTurnTableAndThermostatDialog::reject);
  connect(ui.radioButton_positionMode, &QRadioButton::clicked, this, &SetTurnTableAndThermostatDialog::slot_updatePositionMode);
  connect(ui.radioButton_rateMode, &QRadioButton::clicked, this, &SetTurnTableAndThermostatDialog::slot_updateRateMode);
}
SetTurnTableAndThermostatDialog::~SetTurnTableAndThermostatDialog()
{
}

int SetTurnTableAndThermostatDialog::exec()
{
  //TurnTableRunningStateManager
  showTurnTableInfo();
  //SerialPort
  ui.groupBox_setSerialPort->showSerialPortInfo(serialPortManager);
  return QDialog::exec();
}
void SetTurnTableAndThermostatDialog::accept()
{
  //SerialPort
  ui.groupBox_setSerialPort->setUpSerialPort(serialPortManager);
  QDialog::accept();
}
void SetTurnTableAndThermostatDialog::reject()
{
  showTurnTableInfo();
  ui.groupBox_setSerialPort->showSerialPortInfo(serialPortManager);
  QDialog::reject();
}

void SetTurnTableAndThermostatDialog::showTurnTableInfo()
{
  switch (turnTableRunningStateManager->getState_turnTable_loadMode())
    {
    case TurnTableCodec::Mode_Unloaded:
      ui.radioButton_unloaded->setChecked(true);
      ui.radioButton_loaded->setChecked(false);
      break;
    case TurnTableCodec::Mode_Loaded:
      ui.radioButton_unloaded->setChecked(false);
      ui.radioButton_loaded->setChecked(true);
      break;
    default:
      break;
    }

  switch (turnTableRunningStateManager->getState_turnTable_runningMode())
    {
    case TurnTableCodec::Mode_PositionMode:
      ui.radioButton_positionMode->setChecked(true);
      ui.radioButton_rateMode->setChecked(false);
      break;
    case TurnTableCodec::Mode_RateMode:
      ui.radioButton_positionMode->setChecked(false);
      ui.radioButton_rateMode->setChecked(true);
      break;
    default:
      break;
    }
  slot_updatePositionMode();
  slot_updateRateMode();
  slot_updateTemperature();
}

TurnTableCodec::TurnTableLoadMode SetTurnTableAndThermostatDialog::getLoadMode()
{
  if (true == ui.radioButton_unloaded->isChecked() && false == ui.radioButton_loaded->isChecked())
    {
      return TurnTableCodec::Mode_Unloaded;
    }
  if (false == ui.radioButton_unloaded->isChecked() && true == ui.radioButton_loaded->isChecked())
    {
      return TurnTableCodec::Mode_Loaded;
    }
  return TurnTableCodec::Mode_Loaded;
}
TurnTableCodec::TurnTableRunningMode SetTurnTableAndThermostatDialog::getRunningMode()
{
  if (true == ui.radioButton_positionMode->isChecked() && false == ui.radioButton_rateMode->isChecked())
    {
      return TurnTableCodec::Mode_PositionMode;
    }
  if (false == ui.radioButton_positionMode->isChecked() && true == ui.radioButton_rateMode->isChecked())
    {
      return TurnTableCodec::Mode_RateMode;
    }
  return TurnTableCodec::Mode_RateMode;
}
float SetTurnTableAndThermostatDialog::getPosition()
{
  return ui.lineEdit_position->text().toFloat();
}
float SetTurnTableAndThermostatDialog::getRate()
{
  return ui.lineEdit_rate->text().toFloat();
}
float SetTurnTableAndThermostatDialog::getAcceleration()
{
  return ui.lineEdit_acceleration->text().toFloat();
}
float SetTurnTableAndThermostatDialog::getTargetTemperature()
{
  return ui.lineEdit_targetTemperature->text().toFloat();
}
float SetTurnTableAndThermostatDialog::getTemperatureRate()
{
  return ui.lineEdit_temperatureRate->text().toFloat();
}
void SetTurnTableAndThermostatDialog::slot_updatePositionMode()
{
  if (ui.radioButton_positionMode->isChecked())
    {
      ui.lineEdit_position->setDisabled(false);
      ui.lineEdit_position->setPosition(turnTableRunningStateManager->getState_turnTable_targetPosition());
      ui.lineEdit_rate->setRate(turnTableRunningStateManager->getState_turnTable_targetRate());
      ui.lineEdit_rate->turnMode(LineEdit_TurnTableRate::PositionMode);
      ui.lineEdit_acceleration->setAcceleration(turnTableRunningStateManager->getState_turnTable_targetAcceleration());
      return;
    }
}
void SetTurnTableAndThermostatDialog::slot_updateRateMode()
{
  if (ui.radioButton_rateMode->isChecked())
    {
      ui.lineEdit_position->setText("---");
      ui.lineEdit_position->setDisabled(true);
      ui.lineEdit_rate->setRate(turnTableRunningStateManager->getState_turnTable_targetRate());
      ui.lineEdit_rate->turnMode(LineEdit_TurnTableRate::RateMode);
      ui.lineEdit_acceleration->setAcceleration(turnTableRunningStateManager->getState_turnTable_targetAcceleration());
      return;
    }
}

void SetTurnTableAndThermostatDialog::slot_updateTemperature()
{
  ui.lineEdit_targetTemperature->setTemperature(turnTableRunningStateManager->getState_thermostat_targetTemperature());
  ui.lineEdit_temperatureRate->setRate(turnTableRunningStateManager->getState_thermostat_temperatureRate());
}
TurnTableCodec* SetTurnTableAndThermostatDialog::getTurnTableCodec()
{
  return turnTableCodec;
}
void SetTurnTableAndThermostatDialog::setTurnTableCodec(TurnTableCodec* turnTableCodec)
{
  this->turnTableCodec = turnTableCodec;
}
TurnTableRunningStateManager* SetTurnTableAndThermostatDialog::getTurnTableRunningStateManager()
{
  return turnTableRunningStateManager;
}
void SetTurnTableAndThermostatDialog::setTurnTableRunningStateManager(TurnTableRunningStateManager* turnTableRunningStateManager)
{
  this->turnTableRunningStateManager = turnTableRunningStateManager;
}
SerialPortManager* SetTurnTableAndThermostatDialog::getSerialPortManager()
{
  return serialPortManager;
}
void SetTurnTableAndThermostatDialog::setSerialPortManager(SerialPortManager* serialPortManager)
{
  this->serialPortManager = serialPortManager;
}
int SetTurnTableAndThermostatDialog::getAccuracy()
{
  return ui.lineEdit_position->getAccuracy();
}
void SetTurnTableAndThermostatDialog::setAccuracy(int accuracy)
{
  ui.lineEdit_position->setAccuracy(accuracy);
  ui.lineEdit_rate->setAccuracy(accuracy);
  ui.lineEdit_acceleration->setAccuracy(accuracy);
  ui.lineEdit_targetTemperature->setAccuracy(accuracy);
  ui.lineEdit_temperatureRate->setAccuracy(accuracy);
}
int SetTurnTableAndThermostatDialog::getTurnTable_positionUpperLimit()
{
  return ui.lineEdit_position->getTurnTablePositionUpperLimit();
}
void SetTurnTableAndThermostatDialog::setTurnTable_positionUpperLimit(int turnTable_positionUpperLimit)
{
  ui.lineEdit_position->setTurnTablePositionUpperLimit(turnTable_positionUpperLimit);
}
int SetTurnTableAndThermostatDialog::getTurnTable_positionLowerLimit()
{
  return ui.lineEdit_position->getTurnTablePositionLowerLimit();
}
void SetTurnTableAndThermostatDialog::setTurnTable_positionLowerLimit(int turnTable_positionLowerLimit)
{
  ui.lineEdit_position->setTurnTablePositionLowerLimit(turnTable_positionLowerLimit);
}
int SetTurnTableAndThermostatDialog::getTurnTable_rateUpperLimit()
{
  return ui.lineEdit_rate->getTurnTableRateUpperLimit();
}
void SetTurnTableAndThermostatDialog::setTurnTable_rateUpperLimit(int turnTable_rateUpperLimit)
{
  ui.lineEdit_rate->setTurnTableRateUpperLimit(turnTable_rateUpperLimit);
}
int SetTurnTableAndThermostatDialog::getTurnTable_rateLowerLimit()
{
  return ui.lineEdit_rate->getTurnTableRateLowerLimit();
}
void SetTurnTableAndThermostatDialog::setTurnTable_rateLowerLimit(int turnTable_rateLowerLimit)
{
  ui.lineEdit_rate->setTurnTableRateLowerLimit(turnTable_rateLowerLimit);
}
int SetTurnTableAndThermostatDialog::getTurnTable_accelerationUpperLimit()
{
  return ui.lineEdit_acceleration->getTurnTableAccelerationUpperLimit();
}
void SetTurnTableAndThermostatDialog::setTurnTable_accelerationUpperLimit(int turnTable_accelerationUpperLimit)
{
  ui.lineEdit_acceleration->setTurnTableAccelerationUpperLimit(turnTable_accelerationUpperLimit);
}
int SetTurnTableAndThermostatDialog::getTurnTable_accelerationLowerLimit()
{
  return ui.lineEdit_acceleration->getTurnTableAccelerationLowerLimit();
}
void SetTurnTableAndThermostatDialog::setTurnTable_accelerationLowerLimit(int turnTable_accelerationLowerLimit)
{
  ui.lineEdit_acceleration->setTurnTableAccelerationLowerLimit(turnTable_accelerationLowerLimit);
}
